<html>

 <head><title>CS 564 PHP Project Search Result Page</title></head>

 <?php echo "<a href=\"https://cs564.cs.wisc.edu/laska/index.html\">Back to main page</a>\n"?><p>

 <body>
   <tr>
     <td colspan="2" align="center" valign="top">
      Here are the search results (searched by title):<br>
       <table border="1" width="75%">
        <tr>
         <td align="center" bgcolor="#cccccc"><b>Name</b></td>
         <td align="center" bgcolor="#cccccc"><b>Type</b></td>
         <td align="center" bgcolor="#cccccc"><b>Rarity</b></td>
        </tr>	
 <?php
   // First check the itemid to see if it has been set
  if (! isset($_POST['name'])) {
    echo "  <h3><i>Error, name not set to an acceptable value</i></h3>\n".
        " <a href=\"https://cs564.cs.wisc.edu/laska/index.html\">Back to main page</a>\n".
	" </body>\n</html>\n";
    exit();
  }
  $name = $_POST['name'];
  // Connect to the Database
  pg_connect('dbname=cs564_f12 host=postgres.cs.wisc.edu') 
	or die ("Couldn't Connect ".pg_last_error()); 
  // Get category name and item counts
  $query = "SELECT * FROM magic_schema.cards where name='".$name."'";
  // Execute the query and check for errors
  $result = pg_query($query);
  if (!$result) {
    $errormessage = pg_last_error();
    echo "Error with query: " . $errormessage;
    exit();
  }
  
  
  // get each row and print it out  
  while($row = pg_fetch_array($result,NULL,PGSQL_ASSOC))  {
    echo "        <tr>";
    echo "\n         <td align=\"center\">";
    echo "\n          ".$row['name'];
    echo "\n         </td>";
    echo "\n         <td align=\"center\">";

    // Set more readable name
    if ($row['type'] == 'c') {
      //echo "\n         <td align=\"center\" bgcolor=\"#cc0ccc\">";
      echo "\n          Creature";
    }
    else if ($row['type'] == 'l') {

      echo "\n          Land";
    }
    else if ($row['type'] == 's') {
    //echo "\n         <td align=\"center\">";
      echo "\n          Spell";
    }

    // Formatting things
    echo "\n         </td>";
    //    echo "\n         <td align=\"center\">";

    // Set more readable name
    if ($row['rarity'] == 'c') {
      echo "\n         <td align=\"center\">";
      echo "\n          Common";
    }
    else if ($row['rarity'] == 'u') {
      echo "\n         <td align=\"center\" bgcolor=\"#A4A4A4\">";
      echo "\n          Uncommon";
    }
    else if ($row['rarity'] == 'r') {
      echo "\n         <td align=\"center\" bgcolor=\"#FFBF00\">";
      echo "\n          Rare";
    }
    else if ($row['rarity'] == 'm') {
      echo "\n         <td align=\"center\" bgcolor=\"#DF3A01\">";
      echo "\n          Mythic Rare";
    }

    // Formatting things
    echo "\n         </td>";
    echo "\n        </tr>";
  }
  pg_close();
?>
 </table>
     </td>
    </tr>
        <?php echo "<a href=\"https://cs564.cs.wisc.edu/laska/index.html\">Back to main page</a>\n"?>
 </body>

</html>
